package com.yd.app.mapper;

import com.yd.app.mapper.provider.OrderProvider;
import com.yd.domain.business.Coupon;
import com.yd.domain.business.Order;
import com.yd.domain.business.OrderCoupon;
import com.yd.domain.business.OrderGoods;
import com.yd.domain.searchParam.OrderParam;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 订单
 * Created by sunxianhui on 2018/3/7.
 */
@Repository
public interface OrderMapper {
    /**
     * 根据条件分页查询
     * @param orderParam
     * @param startRow
     * @param pageSize
     * @return
     */
    @SelectProvider(method = "searchOrderList", type = OrderProvider.class)
    List<Order> getOrderList(@Param("orderParam") OrderParam orderParam, @Param("startRow") int startRow, @Param("pageSize") int pageSize);

    /**
     * 根据条件查询数量
     * @param orderParam
     * @return
     */
    @SelectProvider(method = "searchOrderCount", type = OrderProvider.class)
    int getOrderCount(@Param("orderParam") OrderParam orderParam);

    /**
     * 添加订单
     * @param order
     */
    @Insert("insert into business_order  (orderId,userId,status,createTime,dealTime,expressCode,payStatus,ifCoupon,totalPrice,totalWeight,dealPrice,userShow,managerShow,content) values  (#{order.orderId},#{order.userId},#{order.status},#{order.createTime},#{order.dealTime},#{order.expressCode},#{order.payStatus},#{order.ifCoupon},#{order.totalPrice},#{order.totalWeight},#{order.dealPrice},true,true,#{order.content})")
    int addOrder(@Param("order") Order order);

    /**
     * 添加订单——商品
     * @param orderGoods
     */
    @Insert("insert into business_order_goods  (orderId,goodsId,goodsWeightId,dealPrice,createTime,dealNum,perWeight,content) values " +
            " (#{orderGoods.orderId},#{orderGoods.goodsId},#{orderGoods.goodsWeightId},#{orderGoods.dealPrice},#{orderGoods.createTime},#{orderGoods.dealNum},#{orderGoods.perWeight},#{orderGoods.content})")
    void addOrderGoods(@Param("orderGoods") OrderGoods orderGoods);

    /**
     * 添加订单——商品
     * @param orderCoupon
     */
    @Insert("insert into business_order_coupon  (orderId,couponId) values  (#{orderCoupon.orderId},#{orderCoupon.couponId})")
    boolean addOrderCoupon(@Param("orderCoupon") OrderCoupon orderCoupon);
    /**
     * 删除
     */
    @Delete("delete from business_user_coupon where userId=#{userId} and couponId=#{couponId}")
    void deleteOrderCouponUser(@Param("userId")String userId,@Param("couponId")String couponId);
    /**
     * @Author: zy
     * @Desc: 修改用户优惠券数量
     * @Date:  2018/4/11
     * @Param
     */
    @Delete("update  business_user_coupon set number=#{number} where userId=#{userId} and couponId=#{couponId}")
    void updateOrderCouponUser(@Param("userId")String userId,@Param("couponId")String couponId,@Param("number")int number);
    /**
     * 删除
     * @param id
     */
    @Delete("delete from business_order where orderId=#{orderId}")
    int deleteOrderById(@Param("orderId") String id);

    /**
     * 修改
     * @param order
     */
    @Update("update business_order set orderId=#{order.orderId},userId=#{order.userId},status=#{order.status}," +
            "createTime=#{order.createTime},dealTime=#{order.dealTime},expressCode=#{order.expressCode},payStatus=#{order.payStatus}," +
            "ifCoupon=#{order.ifCoupon}, totalPrice=#{order.totalPrice}, dealPrice=#{order.dealPrice} where orderId=#{order.orderId}")
    int updateOrder(@Param("order") Order order);

    /**
     * 根据id条件查询
     * @param id
     * @return
     */
    @Select("select o.*,u.realName as userName,u.companyName,u.phone  FROM business_order o left join sys_user u on o.userId=u.id where orderId=#{orderId}")
    Order selectOrderById(@Param("orderId") String id);

    /**
     * 审核订单状态
     * @param orderId
     * @param status
     */
    @Update("update business_order set status=#{status} where orderId=#{orderId}")
    void reviewOrder(@Param("orderId") String orderId,@Param("status") String status);

    /**
     * 修改支付状态
     * @param orderId
     * @param payStatus
     */
    @Update("update business_order set payStatus=#{payStatus} where orderId=#{orderId}")
    void payStatus(@Param("orderId") String orderId,@Param("payStatus") String payStatus);

    /**
     * 修改快递单号
     * @param orderId
     * @param expressCode
     */
    @Update("update business_order set expressCode=#{expressCode} where orderId=#{orderId}")
    void expressCode(@Param("orderId") String orderId,@Param("expressCode") String expressCode);

    /**
     * 修改快递单号
     * @param order
     */
    @Update("update business_order set expressCode=#{order.expressCode},expressage=#{order.expressage} where orderId=#{order.orderId}")
    void updateExpressCode(@Param("order") Order order);

    /**
     * 根据订单号orderId查询订单下商品
     * @param orderId
     * @return
     */
    @Select("select og.*,g.title goodsTitle,g.ifPic,g.iconUrl,g.identifier,bgw.minWeight,bgw.maxWeight,bgw.jgf,g.typeOne,g.type,t.typeName " +
            "from business_order_goods og " +
            "left join business_goods g on g.id=og.goodsId " +
            "left join business_gtype t on t.id=g.type " +
            "left join business_goods_weight bgw on og.goodsWeightId=bgw.id " +
            "where orderId=#{orderId} order by g.typeOne,g.type,g.gstyle,g.px ")
    List<OrderGoods> selectGoodsByOrderId(@Param("orderId") String orderId);

    @Select("select * from business_order where orderId=#{orderId} ")
    Order getOrderById(@Param("orderId") String orderId);

    @Update("update  business_order set managerShow=false where orderId=#{orderId} ")
    void hideOrderByIdForManager(@Param("orderId") String orderId);
    @Update("update  business_order set userShow=false where orderId=#{orderId} ")
    void hideOrderByIdForUser(@Param("orderId") String orderId);
    @Select("SELECT c.* from business_coupon c where  c.id=(SELECT bc.couponId from business_order_coupon bc where bc.orderId=#{orderId}  )")
    Coupon getCoupon(@Param("orderId") String orderId);
    @Update("UPDATE business_order_goods og set og.dealPrice=(SELECT gt.price from business_gtype gt" +
            " where gt.id=(SELECT bg.type from business_goods bg where bg.id=#{goodsId}))" +
            " where og.orderId=#{orderId} and og.goodsId=#{goodsId}")
    void updatePrice(@Param("orderId") String orderId,@Param("goodsId") String goodsId);

    /**
     * 修改配货状态
     * @param goods
     */
    @Update("update business_order_goods set phStatus=#{goods.phStatus} where orderId=#{goods.orderId} and goodsId=#{goods.goodsId} and goodsWeightId=#{goods.goodsWeightId} ")
    void updatePhStatus(@Param("goods") OrderGoods goods);
}
